package com.nova.bluetooth.ui.device;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.nova.bluetooth.MyApp;
import com.nova.bluetooth.R;
import com.nova.bluetooth.base.BaseFragment;
import com.nova.bluetooth.databinding.FragmentKeyFunctionBinding;
import com.nova.bluetooth.helpUtils.BluetoothDataUtil;
import com.nova.bluetooth.helpUtils.LogUtil;
import com.nova.bluetooth.helpUtils.OnClickEvent;
import com.nova.bluetooth.manage.BluetoothHelper;
import com.nova.bluetooth.myEnum.KeyFunctionClickModel;
import com.nova.bluetooth.myEnum.KeyFunctionControlModel;
import com.nova.bluetooth.ui.bluetoothDemo.NovaBluetoothControlViewModel;
import com.nova.bluetooth.widget.KeyFunctionPickerCustom;

/**
 *
 */
public class KeyFunctionFragment extends BaseFragment {
    private FragmentKeyFunctionBinding binding;
    NovaBluetoothControlViewModel deviceViewModel;
    private KeyFunctionControlModel keyFunctionControlModel_Single_L = KeyFunctionControlModel.VolumeUp;
    private KeyFunctionControlModel keyFunctionControlModel_Single_R = KeyFunctionControlModel.VolumeDown;
    private KeyFunctionControlModel keyFunctionControlModel_Two_L = KeyFunctionControlModel.Previous;
    private KeyFunctionControlModel keyFunctionControlModel_Two_R = KeyFunctionControlModel.Next;
    private KeyFunctionControlModel keyFunctionControlModel_Three_L = KeyFunctionControlModel.PlayOrPause;
    private KeyFunctionControlModel keyFunctionControlModel_Three_R = KeyFunctionControlModel.PlayOrPause;
    private KeyFunctionControlModel keyFunctionControlModel_Long_L = KeyFunctionControlModel.Previous;
    private KeyFunctionControlModel keyFunctionControlModel_Long_R = KeyFunctionControlModel.Next;

    private KeyFunctionClickModel mKeyFunctionClickModel; //选择key键按键类型
    private KeyFunctionControlModel mKeyFunctionControlModel; //选择key键功能
    private boolean isLeftUI = true;//默认左键展示UI

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_key_function, container, false);
        binding = FragmentKeyFunctionBinding.bind(view);
        binding.setOnClick(onClickEvent);
        deviceViewModel = new ViewModelProvider(MyApp.getInstance()).get(NovaBluetoothControlViewModel.class);
        initData();
        initView();
        observe();
        return binding.getRoot();
    }

    private void initData() {
        BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION1, true);
        BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION2, true);
        BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION3, true);
        BluetoothHelper.getInstance().sendDataSafety(BluetoothDataUtil.BLE_CMD_GET_KEY_FUNCTION4, true);
    }

    private void initView() {
        if (isLeftUI){
            updateLeftUI();
        }else {
            updateRightUI();
        }
    }

    private void observe() {
    }

    private void updateLeftUI(){
        binding.tvLeft.setAlpha(1f);
        binding.tvRight.setAlpha(0.5f);

        binding.tvSingleFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Single_L));
        binding.tvTwoFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Two_L));
        binding.tvThreeFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Three_L));
        binding.tvLongFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Long_L));
    }



    private void updateRightUI(){
        binding.tvLeft.setAlpha(0.5f);
        binding.tvRight.setAlpha(1f);

        binding.tvSingleFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Single_R));
        binding.tvTwoFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Two_R));
        binding.tvThreeFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Three_R));
        binding.tvLongFunction.setText(getTextByFunctionModel(keyFunctionControlModel_Long_R));
    }

    private String getTextByFunctionModel(KeyFunctionControlModel model){
        switch (model){
            case VolumeUp:
                return "音量加";
            case VolumeDown:
                return "音量减";
            case Previous:
                return "上一曲";
            case Next:
                return "下一曲";
            case PlayOrPause:
                return "播放/暂停";
        }
        return "";
    }



    private void showFunctionPicker() {
        KeyFunctionPickerCustom pickerCustom = new KeyFunctionPickerCustom(getActivity(), (data) -> {
            // 确定按钮的回调
            LogUtil.d("KeyFunctionPickerCustom " + data);
        });
        // 弹出
        pickerCustom.setValue(1);
        pickerCustom.show();
    }


    private OnClickEvent onClickEvent = new OnClickEvent() {
        @Override
        public void singleClick(View v) {
            if (v.getId() == R.id.iv_back) {
                getActivity().finish();
            } else if (v.getId() == R.id.tv_left) {
                updateLeftUI();
            } else if (v.getId() == R.id.tv_right) {
                updateRightUI();
            } else if (v.getId() == R.id.layout_key_one) {
                showFunctionPicker();
            } else if (v.getId() == R.id.layout_key_two) {

            } else if (v.getId() == R.id.layout_key_three) {

            } else if (v.getId() == R.id.layout_key_long) {

            }
        }
    };
}
